package com.app.conrtoller;

import com.alibaba.fastjson.JSON;
import com.app.domain.ExternalResponse;
import com.app.domain.UploadParameter;
import com.app.domain.YjData;
import com.app.domain.hsy.SendMessage;
import com.app.domain.pzsbehlc.Zlyj_yjsy_m;
import com.app.domain.shhy.HyResultJson;
import com.app.domain.shhy.PostGroutData;
import com.app.domain.shhy.PostMultiGroutSSProcess;
import com.app.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 上海华岩
 */
@Slf4j
@RestController
@RequestMapping("/pzsbehlc")
public class PzsbehlcUploadController {
    //中交平台相关
    @Value("${yj.pzsbehlc.serviceHost}")
    private String serviceHost;
    @Value("${yj.pzsbehlc.processHost}")
    private String processHost;
    @Value("${yj.pzsbehlc.projectId}")
    private String projectId;
    @Value("${yj.pzsbehlc.sectionCode}")
    private String sectionCode;
    @Value("${yj.pzsbehlc.factoryCode}")
    private String factoryCode;
    @Value("${yj.pzsbehlc.equipCode}")
    private String equipCode;
    //广西平台相关
    @Value("${yj.pzsbehlc.serverHost}")
    private String serverHost;
    @Value("${yj.pzsbehlc.sbbh}")
    private String sbbh;
    @Autowired
    YjDataService yjDataService;
    @Autowired
    UploadParameterService uploadParameterService;
    @Autowired
    UploadService uploadService;
    @Autowired
    SendMessageService sendMessageService;
    @Autowired
    PzsbehlcService pzsbehlcService;


    @RequestMapping("/upload")
    @ResponseBody
    public ExternalResponse upload(@RequestParam(value = "ids[]") String[] ids) throws InterruptedException {
        ExternalResponse externalResponse = null;
        int total = ids.length;
        int succTotal = 0;
        int failTotal = 0;
        StringBuffer responseContent = new StringBuffer();
        responseContent.append("本次共上传数据：【" + total + "】条,");
        for (String id : ids) {
            YjData yjData = yjDataService.findOne(id);
            UploadParameter uploadParameter = uploadParameterService.findOne("30");
            PostGroutData postGroutData = uploadService.getPostGroutData(yjData, uploadParameter, projectId, sectionCode, factoryCode);
            postGroutData.setEquipmentCode(equipCode);
            String s = JSON.toJSONString(postGroutData);
            HyResultJson hyResultJson = uploadService.sendShhyMessage(serviceHost, s);
            if (hyResultJson.getSuccess()) {
                PostMultiGroutSSProcess process = uploadService.getPostMultiGroutSSProcess(yjData, uploadParameter, projectId, sectionCode, factoryCode);
                String s1 = JSON.toJSONString(process);
                HyResultJson json = uploadService.sendShhyMessage(processHost, s1);
                if (json.getSuccess()) {
                    updateState("1", String.valueOf(yjData.getId()));
                    succTotal++;
                } else {
                    updateState("0", String.valueOf(yjData.getId()));
                    failTotal++;
                }
            } else {
                updateState("0", String.valueOf(yjData.getId()));
                failTotal++;
            }
        }
        responseContent.append("成功【" + succTotal + "】条,失败" + failTotal + "条");
        externalResponse = new ExternalResponse(0, responseContent.toString(), "");
        return externalResponse;
    }

    @RequestMapping("/uploadZj")
    @ResponseBody
    public ExternalResponse uploadZj(@RequestParam(value = "ids[]") String[] ids) throws InterruptedException {
        ExternalResponse externalResponse = null;
        int total = ids.length;
        int succTotal = 0;
        int failTotal = 0;
        StringBuffer responseContent = new StringBuffer();
        responseContent.append("本次共上传数据：【" + total + "】条,");
        List<YjData> yjDataList = yjDataService.findList(ids);
        UploadParameter uploadParameter = uploadParameterService.findOne("30");
        List<Zlyj_yjsy_m> zlyj_yjsy_m = pzsbehlcService.getZlyj_yjsy_m(yjDataList.get(0), uploadParameter, sbbh);

//        SendMessage sendMessage = sendMessageService.ConditionQuery2(id);
        //主表上传成功
/*        SendMessage sendMessage = new SendMessage();
        sendMessage.setSbbh("2022010901");
        sendMessage.setLh(yjData.getBridge_code());
        sendMessage.setKh(yjData.getDuct_code());
        sendMessage.setBusi_type("SWYJSY_M");
        sendMessage.setUpload_time(new Date());
        sendMessage.setUuid(yjsy_m.get(0).getSyjid());
        sendMessage.setStatus("0");
        sendMessageService.insert(sendMessage);*/
        for (YjData yjData : yjDataList) {





        }
        responseContent.append("成功【" + succTotal + "】条,失败" + failTotal + "条");
        externalResponse = new ExternalResponse(0, responseContent.toString(), "");
        return externalResponse;
    }

    private void updateState(String state, String id) {
        yjDataService.updateById(state, id);
    }
}
